package com.yima.baseproj.module.sys.teacher;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yima.baseproj.module.sys.kindergarten.KindergartenEntity;
import com.yima.baseproj.module.sys.teacher.clazz.TeacherClassService;
import com.yima.baseproj.module.sys.teacher.model.TeacherSearchAddModel;
import com.yima.baseproj.module.sys.user.SysUserService;
import com.yima.baseproj.utils.random_num.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class TeacherService extends ServiceImpl<TeacherMapper,TeacherEntity> {

    @Autowired
    private TeacherClassService teacherClassService;

    @Autowired
    private SysUserService userService;
    /**
     * 分页+模糊查询
     * @param page
     * @param searchModel
     * @return
     */
    public Page<Map> findAllByPage(Page page, TeacherSearchAddModel searchModel) {

        //获得幼儿园id
        int kgId = userService.findKgIdByUserId();
        if (kgId==0) { //幼儿园id返回0说明不是园长
            return baseMapper.findBySearchVo(page,searchModel);
        } else { //是园长，根据幼儿园id查询该幼儿园对应的教师列表
            return baseMapper.findByKgId(page,kgId,searchModel);
        }
    }

    /**
     * 事务，新增教师的同时向teacher_class表添加信息
     * @param addModel
     */
    @Transactional(rollbackFor = Exception.class)
    public void addTeacher(TeacherSearchAddModel addModel) {
        //生成教师专属编码
        String teacherCode = RandomUtil.getTeacherCode();
        //建一个教师实体
        TeacherEntity teacher = new TeacherEntity();
        teacher.setTeacherEntity(addModel);
        teacher.setTeacherCode(teacherCode);
        //先插入教师表。得到教师id
        baseMapper.insert(teacher);
        //再把教师id和对应的班级id，插入教师_班级表
        baseMapper.insertTeacherClass(teacher.getId(),addModel);
    }

    public TeacherSearchAddModel findById(int id){
        return baseMapper.findById(id);
    }

    /**
     * 事务，在修改teacher表的同时向teacher_class表添加信息
     * @param paramModel
     */
    @Transactional(rollbackFor = Exception.class)
    public void editTeacher(TeacherSearchAddModel paramModel){
        TeacherEntity teacher = baseMapper.selectById(paramModel.getId());
        teacher.setTeacherEntity(paramModel);

        baseMapper.updateById(teacher);
        baseMapper.updateTeacherClassById(teacher.getId(),paramModel);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByIds(Integer[] ids) {
        for (Integer id : ids) {
            //删除teacher_class表信息
            teacherClassService.delByTeacherId(id);
            //删除teacher信息
            baseMapper.deleteById(id);
        }
    }



    public TeacherEntity checkTeacherCodeUnique(String code) {
        return baseMapper.selectOne(Wrappers.<TeacherEntity>query().eq("teacher_code",code));
    }
}
